//
//  KMGetDate.m
//  OpenDoor
//
//  Created by cnmobi on 15/5/21.
//  Copyright (c) 2015年 CNMOBI. All rights reserved.
//

#import "KMGetDate.h"

@implementation KMGetDate



+(NSString *)getNowDate
{
    NSString* date;
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYYMMddHHmmss"];
    date = [formatter stringFromDate:[NSDate date]];
    
    return date;
    
}

+(NSString *)getNowDateWithFormStr:(NSString *)formStr
{
    NSString* str;
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:formStr];
    str = [formatter stringFromDate:[NSDate date]];
    
    return str;
    
}

+ (NSString *)timeSpFromDate:(NSDate *)date
{
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
    
    return timeSp;
}
+ (NSString *)nowTimeSp
{
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    
    //----------将nsdate按formatter格式转成nsstring
//    时间转时间戳的方法:
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
    
    return timeSp;
}

+ (NSString *)timespFromTimeStr:(NSString *)str andFormatter:(NSString *)formStr
{
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:formStr];
    
    NSDate *date = [formatter dateFromString:str];
    
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
    
    return timeSp;
}

+ (NSDate *)dateFromTimeStr:(NSString *)str andFormatter:(NSString *)formStr
{
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:formStr];
    
    NSDate *date = [formatter dateFromString:str];
    
    return date;
}

+(NSString *)timeFromNum:(NSString *)numStr
{
    double lastactivityInterval = [numStr doubleValue];
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm"];
    
    
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:lastactivityInterval];
    
    
    NSString* dateString = [formatter stringFromDate:date];
    
    return dateString;
    
}

+(NSString *)timeFromNum:(NSString *)numStr andFormStr:(NSString *)formStr
{
    double lastactivityInterval = [numStr doubleValue];
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:formStr];
    
    
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:lastactivityInterval];
    
    
    NSString* dateString = [formatter stringFromDate:date];
    
    return dateString;
}

+(NSString *)getDateWithDate:(NSDate *)date andFormStr:(NSString *)formStr
{
    NSString* dateStr;
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:formStr];
    dateStr = [formatter stringFromDate:date];
    
    return dateStr;
    
}


+(NSString *)changeDateWith:(NSString *)dateStr withFormatter:(NSString *)formatterStr toFormatter:(NSString *)newFormatterStr
{

    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:formatterStr];
    NSDateFormatter *newFormatter = [[NSDateFormatter alloc]init];
    [newFormatter setDateStyle:NSDateFormatterMediumStyle];
    
    [newFormatter setTimeStyle:NSDateFormatterShortStyle];
    [newFormatter setDateFormat:newFormatterStr];
    
    
    NSDate* newdate = [formatter dateFromString:dateStr];
    
    NSString * newdateStr = [newFormatter stringFromDate:newdate];
    
    return newdateStr;
    
}
@end
